from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

pages = set()
def get_links(url):
    global pages
    html = urlopen("https://baike.baidu.com"+url)
    bsObj = BeautifulSoup(html, "html.parser")
    try:
        print(bsObj.h1.get_text())
        print(bsObj.find(id="posterCon").findAll("p")[0])
    except AttributeError:
        print("在本页面没找到相应元素！")
    for link in bsObj.findAll("a", href = re.compile("^(/item/)")):
        if 'href' in link.attrs:
            if link.attrs['href'] not in pages:
                newPage = link.attrs['href']
                pages.add(newPage)
                print("-----------------\n")
                print(newPage)
                get_links(newPage)
get_links("/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156")
